package com.friend.chat.page.view.share

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.friend.chat.R
import com.friend.chat.base.BaseActivity
import com.friend.chat.databinding.ActivityShareBinding
import com.friend.chat.page.bean.RoomBean
import com.friend.chat.page.bean.UserBean
import com.friend.chat.page.view.main.ChatRoomListFragment
import com.friend.chat.page.vm.MainViewModel
import com.friend.chat.utils.EmojiUtil
import com.friend.chat.utils.GlideUtils
import com.friend.chat.utils.SPUtils
import com.othershe.combinebitmap.CombineBitmap
import com.othershe.combinebitmap.layout.WechatLayoutManager

class ShareReceiveActivity : BaseActivity<ActivityShareBinding, MainViewModel>() {
    private lateinit var mChatListAdapter:ReceiverChatListAdapter
    override fun yourViewModel(): MainViewModel {
        return MainViewModel()
    }

    override fun yourViewBing(): ActivityShareBinding {
        return ActivityShareBinding.inflate(layoutInflater)
    }

    override fun init() {
        val mUserBean = SPUtils.getInstance().getObject("user") as UserBean
        initRecyclerView()
        mVModel.getRoomList(mUserBean.uid)
    }

    override fun onNetListener() {
        super.onNetListener()
        mVModel.roomListResponse.observe(this) {
            val data = it.data
            mChatListAdapter.setList(data)

        }
    }
    private fun initRecyclerView() {
        val linearLayoutManager = object : LinearLayoutManager(this) {
            override fun canScrollHorizontally(): Boolean {
                return false
            }

            override fun canScrollVertically(): Boolean {
                return true
            }
        }
        mChatListAdapter = ReceiverChatListAdapter()
        mViewBinding.rvShareReceiver.layoutManager = linearLayoutManager
        mViewBinding.rvShareReceiver.adapter = mChatListAdapter

        mChatListAdapter.setOnItemClickListener { adapter, view, position ->


        }
    }
    class ReceiverChatListAdapter :
        BaseQuickAdapter<RoomBean, BaseViewHolder>(R.layout.item_chatlist_share_receiver) {
        override fun convert(holder: BaseViewHolder, item: RoomBean) {
            val headerView = holder.getView<ImageView>(R.id.iv_item_share_header)
            val nameView = holder.getView<TextView>(R.id.tv_item_share_name)

            if (item.roomType ==0){
                GlideUtils.loadRadiusImg1(headerView, item.friendHeader, 4)
                nameView.text = item.friendNickName
            }else if (item.roomType == 1){
                //头像
                val headerUrls = arrayOfNulls<String>(item.headers.size)
                for ((index, header) in item.headers.withIndex()) {
                    headerUrls[index] = header.header
                }

                val builder = CombineBitmap.init(context)
                    .setLayoutManager(WechatLayoutManager()) // 必选， 设置图片的组合形式，支持WechatLayoutManager、DingLayoutManager
                    .setSize(40) // 必选，组合后Bitmap的尺寸，单位dp
                    .setGap(3) // 单个图片之间的距离，单位dp，默认0dp
                    .setGapColor(ContextCompat.getColor(context, R.color.bg_grey_1)) // 单个图片间距的颜色，默认白色
                    //.setPlaceholder() // 单个图片加载失败的默认显示图片
                    .setUrls(*headerUrls) // 要加载的图片url数组
                    .setImageView(headerView) // 直接设置要显示图片的ImageView
                    // 设置“子图片”的点击事件，需使用setImageView()，index和图片资源数组的索引对应
                    .setOnSubItemClickListener { } // 加载进度的回调函数，如果不使用setImageView()方法，可在onComplete()完成最终图片的显示
                builder.build()
                //名称
                nameView.text = item.groupName

            }

        }
    }
}